package com.sellspringcloud.apigateway.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.cors.CorsConfiguration;
import org.springframework.web.cors.UrlBasedCorsConfigurationSource;
import org.springframework.web.filter.CorsFilter;

import java.util.Arrays;

/**
 * 跨域设置
 * @author
 * @create 2019-08-11 13:48
 **/
@Configuration
public class CorsConfig {

    @Bean
    public CorsFilter corsFilter() {
        UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();

        CorsConfiguration configuration = new CorsConfiguration();
        // cookie跨域
        configuration.setAllowCredentials(true);
        // 请求响应头跨域
        configuration.setAllowedHeaders(Arrays.asList("*"));
        // 方法
        configuration.setAllowedMethods(Arrays.asList("*"));
        // 源
        configuration.setAllowedOrigins(Arrays.asList("*"));

        source.registerCorsConfiguration("/**",configuration);
        return new CorsFilter(source);
    }

}